/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.opnlp.opnlp2.datacontroller;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.opnlp.opnlp2.db.mysql.hibernate.entity.DataRow;
import org.opnlp.opnlp2.db.mysql.hibernate.util.HibernateUtil;
import org.opnlp.opnlp2.parser.HTMLCleanerEngine;
import org.opnlp.opnlp2.settings.Configuration;

/**
 *
 * @author test
 */
public enum DataController {

    INSTANCE;
    //staro za test
    //private final String SQLQuery = "select `o`.`idobracanje` AS `ID`,`o`.`tekst` AS `tekst`,concat(`p`.`ime`,' ',`p`.`prezime`) AS `Poslanik`,`o`.`datumsednice` AS `Datum`\n"
//    + " from (`otvoreni_parlament`.`obracanje` `o` left join `otvoreni_parlament`.`poslanik` `p` on((`o`.`idposlanika` = `p`.`idposlanik`)))"
//            + "LIMIT 1000;";

    //novi query, vraca samo mart 2013
    private final String SQLQuery = "select `o`.`idobracanje` AS `ID`,`o`.`tekst` AS `tekst`,concat(`p`.`ime`,' ',`p`.`prezime`) AS `Poslanik`,`o`.`datumsednice` AS `Datum`\n"
            + "from (`otvoreni_parlament`.`obracanje` `o` left join `otvoreni_parlament`.`poslanik` `p` on((`o`.`idposlanika` = `p`.`idposlanik`)))\n"
            + "where YEAR (o.datumsednice) = 2013 and MONTH (o.datumsednice) = 3;";

    private List<DataRow> dataset = new LinkedList<DataRow>();

    public void loadDataFromDatabase() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String sql = SQLQuery;
        SQLQuery query = session.createSQLQuery(sql);
//        query.addEntity(Poslanik.class);
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        List results = query.list();

        for (Object object : results) {
            Map row = (Map) object;
            DataRow data = new DataRow();
            data.setId((Integer) row.get("ID"));
            data.setTekst(row.get("tekst").toString());
            data.setPoslanik(row.get("Poslanik").toString());
            data.setDatum(row.get("Datum").toString());
            dataset.add(data);

//            System.out.println("ID: " + row.get("ID") + "Tekst: " + row.get("tekst") + "Poslanik: " + row.get("Poslanik") + "Datum:" + row.get("Datum"));
//            dataRow = (DataRow) object;
//            System.out.println(dataRow.getId() + "\t" + dataRow.getTekst()+ "\t" + dataRow.getPoslanik()+ "\t" + dataRow.getDatum());
        }
    }

    public void printAllDataOnStandardOutput() {
        for (Iterator<DataRow> it = dataset.iterator(); it.hasNext();) {
            DataRow dataRow = it.next();
            System.out.println(dataRow);
        }
    }

    public void exportAllDataToTxt() {
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Configuration.locationToExportTxt), "UTF-8"));

            for (DataRow dataRow : dataset) {
                int id = dataRow.getId();
                String mp = HTMLCleanerEngine.clean(dataRow.getPoslanik());
                String text = HTMLCleanerEngine.clean(dataRow.getTekst());
                writer.write(id + "#" + mp + "#" + text + "\n");
            }
        } catch (Exception e) {
            System.out.println("Error! \n" + e.getStackTrace());
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
